home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / lib / xlib / pixmap.c < prev    next >
C/C++ Source or Header  |  1992-10-09  |  5KB  |  159 lines

  1. #include "xlib.h"
  2.  
  3. Generic_Predicate (Pixmap)
  4.  
  5. Generic_Equal_Dpy (Pixmap, PIXMAP, pm)
  6.  
  7. Generic_Print (Pixmap, "#[pixmap %u]", PIXMAP(x)->pm)
  8.  
  9. Generic_Get_Display (Pixmap, PIXMAP)
  10.  
  11. static Object Internal_Make_Pixmap (finalize, dpy, pix)
  12.     Display *dpy; Pixmap pix; {
  13.     Object pm;
  14.  
  15.     if (pix == None)
  16.     return Sym_None;
  17.     pm = Find_Object (T_Pixmap, (GENERIC)dpy, Match_X_Obj, pix);
  18.     if (Nullp (pm)) {
  19.     pm = Alloc_Object (sizeof (struct S_Pixmap), T_Pixmap, 0);
  20.     PIXMAP(pm)->tag = Null;
  21.     PIXMAP(pm)->pm = pix;
  22.     PIXMAP(pm)->dpy = dpy;
  23.     PIXMAP(pm)->free = 0;
  24.     Register_Object (pm, (GENERIC)dpy,
  25.         finalize ? P_Free_Pixmap : (PFO)0, 0);
  26.     }
  27.     return pm;
  28. }
  29.  
  30. /* Backwards compatibility: */
  31. Object Make_Pixmap (dpy, pix) Display *dpy; Pixmap pix; {
  32.     return Internal_Make_Pixmap (1, dpy, pix);
  33. }
  34.  
  35. Object Make_Pixmap_Foreign (dpy, pix) Display *dpy; Pixmap pix; {
  36.     return Internal_Make_Pixmap (0, dpy, pix);
  37. }
  38.  
  39. Pixmap Get_Pixmap (p) Object p; {
  40.     Check_Type (p, T_Pixmap);
  41.     return PIXMAP(p)->pm;
  42. }
  43.  
  44. Object P_Free_Pixmap (p) Object p; {
  45.     Check_Type (p, T_Pixmap);
  46.     if (!PIXMAP(p)->free)
  47.     XFreePixmap (PIXMAP(p)->dpy, PIXMAP(p)->pm);
  48.     Deregister_Object (p);
  49.     PIXMAP(p)->free = 1;
  50.     return Void;
  51. }
  52.  
  53. static Object P_Create_Pixmap (d, w, h, depth) Object d, w, h, depth; {
  54.     Display *dpy;
  55.     Drawable dr = Get_Drawable (d, &dpy);
  56.  
  57.     return Make_Pixmap (dpy, XCreatePixmap (dpy, dr, Get_Integer (w),
  58.     Get_Integer (h), Get_Integer (depth)));
  59. }
  60.  
  61. static Object P_Create_Bitmap_From_Data (win, data, pw, ph)
  62.     Object win, data, pw, ph; {
  63.     register w, h;
  64.  
  65.     Check_Type (win, T_Window);
  66.     Check_Type (data, T_String);
  67.     w = Get_Integer (pw);
  68.     h = Get_Integer (ph);
  69.     if (w * h > 8 * STRING(data)->size)
  70.     Primitive_Error ("bitmap too small");
  71.     return Make_Pixmap (WINDOW(win)->dpy,
  72.     XCreateBitmapFromData (WINDOW(win)->dpy, WINDOW(win)->win,
  73.         STRING(data)->data, w, h));
  74. }
  75.  
  76. static Object P_Create_Pixmap_From_Bitmap_Data (win, data, pw, ph, fg, bg,
  77.     depth) Object win, data, pw, ph, fg, bg, depth; {
  78.     register w, h;
  79.  
  80.     Check_Type (win, T_Window);
  81.     Check_Type (data, T_String);
  82.     w = Get_Integer (pw);
  83.     h = Get_Integer (ph);
  84.     if (w * h > 8 * STRING(data)->size)
  85.     Primitive_Error ("bitmap too small");
  86.     return Make_Pixmap (WINDOW(win)->dpy,
  87.     XCreatePixmapFromBitmapData (WINDOW(win)->dpy, WINDOW(win)->win,
  88.         STRING(data)->data, w, h, Get_Pixel (fg), Get_Pixel (bg),
  89.         Get_Integer (depth)));
  90. }
  91.  
  92. static Object P_Read_Bitmap_File (d, fn) Object d, fn; {
  93.     Display *dpy;
  94.     Drawable dr = Get_Drawable (d, &dpy);
  95.     char *filename;
  96.     unsigned width, height;
  97.     int r, xhot, yhot;
  98.     Pixmap bitmap;
  99.     Object t, ret, x;
  100.     GC_Node2;
  101.     Declare_C_Strings;
  102.  
  103.     Make_C_String (fn, filename);
  104.     Disable_Interrupts;
  105.     r = XReadBitmapFile (dpy, dr, filename, &width, &height, &bitmap,
  106.     &xhot, &yhot);
  107.     Enable_Interrupts;
  108.     Dispose_C_Strings;
  109.     if (r != BitmapSuccess)
  110.     return Bits_To_Symbols ((unsigned long)r, 0, Bitmapstatus_Syms);
  111.     t = ret = P_Make_List (Make_Fixnum (5), Null);
  112.     GC_Link2 (ret, t);
  113.     x = Make_Pixmap (dpy, bitmap);
  114.     Car (t) = x; t = Cdr (t);
  115.     Car (t) = Make_Fixnum (width); t = Cdr (t);
  116.     Car (t) = Make_Fixnum (height); t = Cdr (t);
  117.     Car (t) = Make_Fixnum (xhot); t = Cdr (t);
  118.     Car (t) = Make_Fixnum (yhot);
  119.     GC_Unlink;
  120.     return ret;
  121. }
  122.  
  123. static Object P_Write_Bitmap_File (argc, argv) Object *argv; {
  124.     Object file;
  125.     Pixmap pm;
  126.     char *filename;
  127.     int ret, xhot = -1, yhot = -1;
  128.     Declare_C_Strings;
  129.  
  130.     file = argv[0];
  131.     Make_C_String (file, filename);
  132.     pm = Get_Pixmap (argv[1]);
  133.     if (argc == 5)
  134.     Primitive_Error ("both x-hot and y-hot must be specified");
  135.     if (argc == 6) {
  136.     xhot = Get_Integer (argv[4]);
  137.     yhot = Get_Integer (argv[5]);
  138.     }
  139.     Disable_Interrupts;
  140.     ret = XWriteBitmapFile (PIXMAP(argv[1])->dpy, filename, pm,
  141.     Get_Integer (argv[2]), Get_Integer (argv[3]), xhot, yhot);
  142.     Enable_Interrupts;
  143.     Dispose_C_Strings;
  144.     return Bits_To_Symbols ((unsigned long)ret, 0, Bitmapstatus_Syms);
  145. }
  146.  
  147. init_xlib_pixmap () {
  148.     Generic_Define (Pixmap, "pixmap", "pixmap?");
  149.     Define_Primitive (P_Pixmap_Display,    "pixmap-display",    1, 1, EVAL);
  150.     Define_Primitive (P_Free_Pixmap,       "free-pixmap",       1, 1, EVAL);
  151.     Define_Primitive (P_Create_Pixmap,     "create-pixmap",     4, 4, EVAL);
  152.     Define_Primitive (P_Create_Bitmap_From_Data,
  153.             "create-bitmap-from-data",              4, 4, EVAL);
  154.     Define_Primitive (P_Create_Pixmap_From_Bitmap_Data,
  155.             "create-pixmap-from-bitmap-data",       7, 7, EVAL);
  156.     Define_Primitive (P_Read_Bitmap_File,  "read-bitmap-file",  2, 2, EVAL);
  157.     Define_Primitive (P_Write_Bitmap_File, "write-bitmap-file", 4, 6, VARARGS);
  158. }
  159.